package com.commodity.common;

import org.springframework.jdbc.datasource.SimpleDriverDataSource;

import javax.sql.DataSource;
import java.sql.SQLException;

public class Global {
    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String JDBC_URL = "jdbc:mysql://localhost:3306/commodity?characterEncoding=UTF-8&useSSL=false&autoReconnect=true&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";
    private static final String JDBC_USER = "root";
    private static final String JDBC_PASSWORD = "123456";

    private static DataSource dataSource;

    public static DataSource getDataSource(){
        java.sql.Driver driver = null;
        try {
            driver = new com.mysql.cj.jdbc.Driver();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (dataSource == null){
            dataSource = new SimpleDriverDataSource(driver,JDBC_URL,JDBC_USER,JDBC_PASSWORD);
        }
        return dataSource;
    }
}
